<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.search" placeholder="请输入查询内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleAdd">
        添加设备
      </el-button>
      <el-tag type="success" style="margin-left: 10px;">全部:{{ total }}</el-tag>
      <el-tag style="margin-left: 10px;">在线:{{ device_online_num }}</el-tag>
    </div>
    <el-table
      :data="list"
      border
      style="width: 100%;margin-top: 10px;"
    >
      <el-table-column
        prop="id"
        label="序号"
        min-width="15%"
      />
      <el-table-column
        prop="name"
        label="设备名称"
        min-width="25%"
      />
      <el-table-column
        prop="identity"
        label="设备码"
        min-width="25%"
      />
      <el-table-column
        prop="device_group.name"
        label="设备组"
        min-width="25%"
      />
      <el-table-column
        prop="task_num"
        label="待执行任务"
        min-width="15%"
      />
      <el-table-column
        prop="app"
        label="app"
        min-width="30%"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.app === 'tiktok_store'" size="medium">小店</el-tag>
		  <el-tag v-if="scope.row.app === 'tiktok_task'" size="medium">任务</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        min-width="30%"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 'on'" size="medium">在线</el-tag>
          <el-tag v-if="scope.row.status === 'off'" type="danger" size="medium">离线</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="30%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>

          <el-popconfirm
            :title="'确定删除设备 '+scope.row.name+' 吗?'"
            style="margin-left: 10px;"
            @onConfirm="handleDel(scope.$index, scope.row)"
          >
            <el-button
              slot="reference"
              size="mini"
              type="danger"
            >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

      </el-table-column></el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="handleCurrentChange"
    />

    <el-dialog title="创建设备" :visible.sync="addDeviceFormVisible">
      <el-form ref="addDeviceForm" :rules="addDeviceFormRules" :model="addDeviceForm" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="设备名" prop="name">
          <el-input v-model="addDeviceForm.name" />
        </el-form-item>
        <el-form-item label="设备码" prop="identity">
          <el-input v-model="addDeviceForm.identity" />
        </el-form-item>
        <el-form-item label="设备组" prop="group_id">
          <el-select v-model="addDeviceForm.group_id" class="filter-item" placeholder="请选择设备分组">
            <el-option v-for="item in deviceGroupList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDeviceFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="add()">
          确认
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改设备" :visible.sync="editDeviceFormVisible">
      <el-form ref="editDeviceForm" :rules="editDeviceFormRules" :model="editDeviceForm" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
        <el-form-item label="设备名" prop="name">
          <el-input v-model="editDeviceForm.name" />
        </el-form-item>
        <el-form-item label="设备码" prop="identity">
          <el-input v-model="editDeviceForm.identity" />
        </el-form-item>
        <el-form-item label="设备组" prop="group_id">
          <el-select v-model="editDeviceForm.group_id" class="filter-item" placeholder="请选择设备分组">
            <el-option v-for="item in deviceGroupList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDeviceFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="edit()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import { Message } from 'element-ui'
import { getDeviceList, addDevice, editDevice, delDevice, getDeviceGroupList } from '../../api/device.js'

export default {
  data() {
    return {
      list: null,
      total: 0,
      device_online_num: 0,
      deviceGroupTotal: 0,
      listQuery: {
        page: 1,
        search: ''
      },
      addDeviceFormVisible: false,
      addDeviceFormRules: {
        name: [{ required: true, message: '设备名必须填写', trigger: 'blur' }],
        identity: [{ required: true, message: '设备码必须填写', trigger: 'blur' }],
        group_id: [{ required: true, message: '设备分组必须填写', trigger: 'blur' }]
      },
      addDeviceForm: {
        name: '',
        identity: '',
        group_id: ''
      },
      editDeviceFormVisible: false,
      editDeviceFormRules: {
        name: [{ required: true, message: '设备名必须填写', trigger: 'blur' }],
        identity: [{ required: true, message: '设备码必须填写', trigger: 'blur' }],
        group_id: [{ required: true, message: '设备分组必须填写', trigger: 'blur' }]
      },
      editDeviceForm: {
        id: '',
        name: '',
        identity: '',
        group_id: ''
      },
      deviceGroupList: {}
    }
  },
  created() {
    this.getDeviceList()
  },
  methods: {
    getDeviceList() {
      this.listLoading = true
      getDeviceList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.device_online_num = response.data.device_online_num
      })
    },
    getDeviceGroupList() {
      getDeviceGroupList({ pageSize: 999999 }).then(response => {
        if (response.code === 1) {
          this.deviceGroupList = response.data.data
        }
      })
    },
    resetRow() {
      this.row = {
        id: '',
        identity: '',
        name: '',
        group: ''
      }
    },
    resetDeviceGroupList() {
      this.deviceGroupList = {}
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getDeviceList()
    },
    handleAdd() {
      this.getDeviceGroupList()
      this.resetDeviceGroupList()
      this.resetRow()
      this.addDeviceFormVisible = true
      this.$nextTick(() => {
        this.$refs['addDeviceForm'].clearValidate()
      })
    },
    handleEdit(index, row) {
      this.resetDeviceGroupList()
      this.getDeviceGroupList()
      this.editDeviceForm.id = row.id
      this.editDeviceForm.name = row.name
      this.editDeviceForm.identity = row.identity
      this.editDeviceForm.group_id = row.group_id
      this.editDeviceFormVisible = true
    },
    handleDel(index, row) {
      this.delDevice(row.id)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getDeviceList()
    },
    add() {
      this.$refs['addDeviceForm'].validate((valid) => {
        if (valid) {
          addDevice(this.addDeviceForm).then(response => {
            if (response.code === 1) {
              Message({
                message: response.msg,
                type: 'success',
                duration: 5 * 1000
              })
              this.addDeviceFormVisible = false
              this.getDeviceList()
            }
          })
        }
      })
    },
    del(id) {
      delDevice({ id: id }).then(response => {
        if (response.code === 1) {
          Message({
            message: response.msg,
            type: 'success',
            duration: 5 * 1000
          })
          this.addDeviceFormVisible = false
          this.getDeviceList()
        }
      })
    },
    edit() {
      editDevice(this.editDeviceForm).then(response => {
        if (response.code === 1) {
          Message({
            message: response.msg,
            type: 'success',
            duration: 5 * 1000
          })
          this.editDeviceFormVisible = false
          this.getDeviceList()
        }
      })
    }

  }
}
</script>
